package com.wanxi.servlet;


import com.wanxi.model.CompanyModel;
import com.wanxi.model.NavModel;
import com.wanxi.model.ProductModel;
import com.wanxi.model.UserModel;
import com.wanxi.service.CompanyService;
import com.wanxi.service.NavService;
import com.wanxi.service.ProductService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
import java.util.List;
//restful：get查询 put 添加 post修改 delete删除

@WebServlet("/home")
public class HomeServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println(req.getMethod());
//        System.out.println(req.getContextPath());
//        System.out.println(req.getRequestURI());
//        System.out.println(req.getRequestURL());
//        System.out.println(req.getQueryString());
//        Enumeration<String> headerNames = req.getHeaderNames();
//        req.getHeader("connect");
//       while (headerNames.hasMoreElements()){
//           String s = headerNames.nextElement();
//           System.out.println(s);
//       }
//        BufferedReader reader = req.getReader();
//        String read = null;
//        while ((read = reader.readLine()) != null) {
//            System.out.println(read);
//        }
//        req.setCharacterEncoding("UTF-8");
//        resp.setCharacterEncoding("UTF-8");
//        String name = req.getParameter("name");
//        new String(name.getBytes(StandardCharsets.UTF_8),"GBK")
//        String pass = req.getParameter("pass");
//        UserModel userModel=new UserModel();
//        userModel.setName(name);
//        userModel.setPass(pass);
//        UserModel userModel = new UserModel(name, pass);
//        System.out.println(userModel);
//        req.setAttribute("username",name);
//        req.setAttribute("password",pass);
//        req.setAttribute("userModel", userModel);
//        System.out.println(req.getAttribute("username"));
// 需要将数据响应给前端
//        页面转发：可以共享数据
//        req.getRequestDispatcher("/Login.jsp").forward(req, resp);
//        重定向：只是单纯的跳转页面，无法数据共享
//        resp.sendRedirect("/Login.jsp");
//   1.乱码处理
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
//        2.拿到前端的值
//        3.将前端的值封装到对象
//        4.调用服务逻辑层
        CompanyService companyService = new CompanyService();
        CompanyModel companyModel = companyService.getCompanyModel();

        NavService navService = new NavService();
        List<NavModel> navModelList = navService.getNavModelList();


        ProductService productService = new ProductService();
        List<ProductModel> productModelList = productService.getProductModelList();
//        req.setAttribute("phone","028-99999999");
//        req.setAttribute("logo", "../img/logo_g0yv.jpg");
//        5.将数据得到存入req，并转发到页面
        req.setAttribute("companyModel", companyModel);

        req.setAttribute("navModelList", navModelList);
        req.setAttribute("productModelList", productModelList);
        req.getRequestDispatcher("/jsp/home.jsp").forward(req, resp);
    }
}